package org.springonrails.model;

import java.util.ArrayList;
import java.util.List;

public class Relationship {
	
	private String typeString;	
	private String[] properties;
	private Target target;
	private Intermediate intermediate;

	private List<RelationshipColumnPair> leftColumnPairs = new ArrayList<RelationshipColumnPair>();
	private List<RelationshipColumnPair> rightColumnPairs = new ArrayList<RelationshipColumnPair>();
	private Entity targetEntity;
	private Entity interEntity;

	
	
	public boolean isHasMultipleTarget()
	{
		if(typeString==null) return true;		
		return (typeString.equalsIgnoreCase("one-to-many") || typeString.equalsIgnoreCase("many-to-many"));
	}
	
	public Entity getInterEntity() {
		return interEntity;
	}

	public void setInterEntity(Entity interEntity) {
		this.interEntity = interEntity;
	}

	public boolean isHasIntermediate() {
		return intermediate != null;
	}

	public Entity getTargetEntity() {
		return targetEntity;
	}

	public void setTargetEntity(Entity targetEntity) {
		this.targetEntity = targetEntity;
	}

	public List<RelationshipColumnPair> getLeftColumnPairs() {
		return leftColumnPairs;
	}

	public void addLeftColumnPair(RelationshipColumnPair leftColumnPair) {
		this.leftColumnPairs.add(leftColumnPair);
	}

	public boolean isHasLeftColumnPairs() {
		return leftColumnPairs.size() > 0;
	}

	public boolean isHasRightColumnPairs() {
		return rightColumnPairs.size() > 0;
	}

	public List<RelationshipColumnPair> getRightColumnPairs() {
		return rightColumnPairs;
	}

	public void addRightColumnPair(RelationshipColumnPair rightColumnPair) {
		this.rightColumnPairs.add(rightColumnPair);
	}

	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("<relationship type=\"").append(typeString).append("\"");
		buf.append(" properties=\"");
		for (int i = 0; i < properties.length; i++) {
			buf.append(properties[i]);
			if (i < (properties.length - 1))
				buf.append(",");
		}
		buf.append("\"");
		buf.append(">\r\n");
		buf.append(target.toString());
		buf.append("\r\n");
		if (intermediate != null)
			buf.append(intermediate.toString());
		buf.append("</relationship>\r\n");
		return buf.toString();
	}

	public String getTypeString() {
		return typeString;
	}

	public void setTypeString(String typeString) {
		this.typeString = typeString;
	}

	public String[] getProperties() {
		return properties;
	}

	public void setProperties(String[] properties) {
		this.properties = properties;
	}

	public Target getTarget() {
		return target;
	}

	public void setTarget(Target target) {
		this.target = target;
	}

	public Intermediate getIntermediate() {
		return intermediate;
	}

	public void setIntermediate(Intermediate intermediate) {
		this.intermediate = intermediate;
	}

}
